// chapter detail page images url fetch
var request = require('request');
var querystringify = require('querystringify');
var fs = require('fs');
var path = require('path');
var async = require('async');


var driver = require('../../dao/db');
var utils = require('../../dao/utils');
const url = "mongodb://localhost:27017/manhua";
const table = 'manhua';
var lastId;
var rootPath = path.resolve(__dirname, '../../manhuaHub');

// console.log(rootPath);

start();


// 网页数据提取
function matchData(html) {
    var DM5_VIEWSIGN = /\bDM5_VIEWSIGN\b="(.*?)";/ig;
    var DM5_VIEWSIGN_DT = /\bDM5_VIEWSIGN_DT\b="(.*?)";/ig;
    var DM5_MID = /\bDM5_MID\b=(.*?);/ig;
    var DM5_CID = /\bDM5_CID\b=(.*?);/ig;
    var DM5_IMAGE_COUNT = /\bDM5_IMAGE_COUNT\b=(.*?);/ig;


    var _sign = DM5_VIEWSIGN.exec(html);
    var _dt = DM5_VIEWSIGN_DT.exec(html);
    var _mid = DM5_MID.exec(html);
    var _cid = DM5_CID.exec(html);
    var totalPage = DM5_IMAGE_COUNT.exec(html);


    return {
        totalPage: Number(totalPage[1]),
        cid: Number(_cid[1]),
        page: 1,
        key: '',
        language: 1,
        gtk: 6,
        _cid: Number(_cid[1]),
        _mid: Number(_mid[1]),
        _dt: _dt[1],
        _sign: _sign[1]
    };

}

function start() {
    var query = {};
    if (lastId) {
        query['_id'] = { $gt: lastId };
    }

    async.waterfall([
        // 连接数据库        
        function (callback) {
            driver.connect(callback, url);
        },
        // 查找
        function (db, callback) {
            driver.pageById(db, callback, table, query, { "chapters": 1, "_id": 1 }, 1);
        },
        function (db, res, callback) {
            driver.close(db, res, callback);
        }
    ],
        // result
        function (err, result) {
            // console.log("resulte");

            if (err) {
                console.log(err);
                return;
            }
            if (result.success) {

                var data = result.data[0];

                lastId = data['_id'];

                var chapters = data['chapters'];

                // console.log(chapters);

                for (var i in chapters) {
                    var href = chapters[i]['href'];

                    var folderName = chapters[i]['chapter'];

                    fetchWebPage(href, folderName);
                }


            }
        }
    );

}


function fetch(url, folderName) {

    async.waterfall([
        // 获取漫画第几话的网页       
        function (callback) {
            request({ url: url, encoding: null, timeout: 8000 }, function (err, res, body) {

                var html = body.toString('utf-8');
                // 超时错误处理
                if (err) {

                    console.log(err);
                    callback({ error: err, body: body }, {});
                    return;
                }

                // console.log(html);
                console.log('---x-x-x-');
                console.log(matchData(html));
                var param = matchData(html);
                callback(null, param);
            });
        },
        // 请求图片url地址获取
        function (param, callback) {


            var fileName = param.page;

            var options = {
                url: url + "chapterfun.ashx?" + querystringify.stringify(param),
                headers: {
                    'Referer': url
                }
            };
            request(options, function (err, res, body) {
                var arry;
                try {
                    arry = eval(body);
                } catch (e) {
                    console.log('error:');
                    console.log(e);
                    callback({ error: e, body: body });
                }


                if (arry.length > 0) {
                    callback(url, fileName, arry);
                } else {
                    callback({ error: 'length = 0', body: body });
                }

                console.log(arry);
            });




        }
        // 将图片保存到本地
        /*, function (Refererurl, fileName, arry, callback) {

            var url = arry[0];

            console.log("uuu:" + url);
            console.log(Refererurl);

            var options = {
                url: url,
                headers: {
                    'Referer': Refererurl
                }
            };


            
            request(options, function (err, res, body) {
                console.log('what happen!');
                console.log(err);

                if (err) {
                    callback({ error: err, body: body });
                } else {

                    // console.log(res);
                    callback(null, fileName, body.toString('base64'));
                }
            });
        },
        function (fileName, buffer, callback) {

            if (!fs.existsSync(rootPath + "/" + folderName)) {
                fs.mkdirSync(rootPath + "/" + folderName);
            }
            console.log(buffer);
            buffer = new Buffer(buffer, 'base64');
            console.log(buffer);

            // 生成图片2(把buffer写入到图片文件)
            fs.writeFile(rootPath + "/" + folderName + '/' + fileName + '.jpg', buffer, function (err) {
                if (err) {
                    console.log(err);
                    callback({ error: err, body: err });
                } else {
                    callback(null, { success: true });
                }
            });
        }*/
    ], function (err, Refererurl, fileName, arry) {
        if (err) {
            console.log('结果出错了！！！');
            console.log(err);

        } else {

            // 创建文件夹
            var _path = rootPath + "/" + lastId + "/" + folderName + '/' + fileName + '.jpg';

            if (!fs.existsSync(rootPath + "/" + lastId)) {
                fs.mkdirSync(rootPath + "/" + lastId);
            }
            if (!fs.existsSync(rootPath + "/" + lastId + "/" + folderName)) {
                fs.mkdirSync(rootPath + "/" + lastId + "/" + folderName);
            }


            // 
            fs.writeFile(_path, function (err) {
                if (err) {
                    return console.log(err);
                }
                console.log("The file was saved!");
            });

            var imgUrl = arry[0];

            console.log("uuu:" + url);
            console.log(Refererurl);

            var options = {
                url: imgUrl,
                headers: {
                    'Referer': Refererurl
                }
            };

            request(options).on('error', function (err) {
                console.log(err);
            }).pipe(fs.createWriteStream(_path));


        }
    });
}



function fetchWebPage(url, folderName) {
    request({ url: url, encoding: null, timeout: 8000 }, function (err, res, body) {

        var html = body.toString('utf-8');
        // 超时错误处理
        if (err) {

            console.log(err);
            callback({ error: err, body: body }, {});
            return;
        }

        // console.log(html);
        console.log('---x-x-x-');
        console.log(matchData(html));
        var param = matchData(html);
        pageNext(url, folderName, param);
    });
}

// manhua next
function pageNext(url, folderName, param) {


    var totalPage = param.totalPage;
    for (var i = 1; i <= totalPage; i++) {

        (function (i) {
            param.page = i;
            i= null;

            var fileName = param.page;

            var options = {
                url: url + "chapterfun.ashx?" + querystringify.stringify(param),
                headers: {
                    'Referer': url
                }
            };
            request(options, function (err, res, body) {
                var arry;
                try {
                    arry = eval(body);
                } catch (e) {
                    console.log('error:');
                    console.log(e);
                }


                if (arry.length > 0) {
                    storeImage(url, fileName, folderName, arry);
                } else {
                    console.log('length = 0');
                }

                console.log(arry);
            });
        })(i);

    }
}


function storeImage(url, fileName, folderName, arry) {

    // 创建文件夹
    var _path = rootPath + "/" + lastId + "/" + folderName + '/' + fileName + '.jpg';

    if (!fs.existsSync(rootPath + "/" + lastId)) {
        fs.mkdirSync(rootPath + "/" + lastId);
    }
    if (!fs.existsSync(rootPath + "/" + lastId + "/" + folderName)) {
        fs.mkdirSync(rootPath + "/" + lastId + "/" + folderName);
    }


    // 
    fs.writeFile(_path, function (err) {
        if (err) {
            return console.log(err);
        }
        console.log("The file was saved!");
    });

    var imgUrl = arry[0];

    console.log("uuu:" + url);

    var options = {
        url: imgUrl,
        headers: {
            'Referer': url
        }
    };

    request(options).on('error', function (err) {
        console.log(err);
    }).pipe(fs.createWriteStream(_path));

}

